package cn.itsource.celljava.system.register.controller;

import cn.itsource.celljava.constants.BaseConstants;
import cn.itsource.celljava.system.logininfo.mapper.LogininfoMapper;
import cn.itsource.celljava.system.register.service.IRegisterService;
import cn.itsource.celljava.system.shop.domain.Shop;
import cn.itsource.celljava.system.shop.mapper.ShopMapper;
import cn.itsource.celljava.system.user.domain.User;
import cn.itsource.celljava.system.user.service.IUserService;
import cn.itsource.celljava.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;

@RestController
@RequestMapping("/register")
@Api(value = "注册相关",description = "商家和用户注册")
public class RegisterController {
    @Autowired
    private IRegisterService registerService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private ShopMapper shopMapper;


    public void send(Shop shop) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1137358239@qq.com");
        helper.setSubject("入住激活");
        //第二个参数 true 代表是否是一个html内容
        String a=shop.getAdmin().getEmail();
        Long b=shop.getAdminId();
        helper.setText("<h1>入住激活</h1>"+
                "<a herf='http://localhost:8080/register/+"+b+"'/>",true);

        //收件人
        helper.setTo(a);
        System.out.println("http://localhost:8080/register/"+b);
        //发送邮件
        javaMailSender.send(mimeMessage);
    }


    @RequestMapping(value = "/{a}")
    @ApiOperation("激活")
    public AjaxResult change(@PathVariable("a") Long a){
        System.out.println(a+"+++++"+BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        try {

            logininfoMapper.change(a,BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
            System.out.println(shopMapper.findOne(41L));
            return AjaxResult.success().setMsg("激活成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail().setMsg(e.getMessage());
        }
    }

    @PutMapping("/shop")
    @ApiOperation("商家注册")
    public AjaxResult shop(@RequestBody Shop shop){
        try {
            registerService.shopRegister(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail().setMsg(e.getMessage());
        }
    }

    @PostMapping("/phone")
    @ApiOperation("手机验证码登录")
    public AjaxResult phone(@RequestBody HashMap<String,String> hashMap){
        String phone = hashMap.get("phone");
        String code = hashMap.get("code");
        String o = (String) redisTemplate.opsForValue().get(phone+ BaseConstants.Suffix.STATE_PHONE);
        if (o==null){
            return AjaxResult.fail().setMsg("请重新获取短信验证码");
        }
        if (!o.equals(code)){
            return AjaxResult.fail().setMsg("验证码错误");
        }
        //检索当前手机号是否已注册
        Long count = userService.findByPhone(Long.valueOf(phone));
        if (count==0){
            //未注册,把信息添加到user和logininfo中
            User user = new User();
            user.setPhone(phone);
            user.setUsername(phone);
            user.setState(BaseConstants.User.STATE_NORMAL);
            userService.add(user);
        }
        return AjaxResult.success();
    }

}
